'''
    BasePage类：用于实现Selenium的操作行为封装
'''
from time import sleep

import ddddocr
from selenium import webdriver


class BasePage:
    # driver = webdriver.Chrome()

    # 构造方法
    def __init__(self, driver):
        self.driver = driver    # 确保每一个完整流程都是基于同一个driver对象来实现的。
        self.driver.implicitly_wait(5)

    # 访问url
    def open(self, url):
        self.driver.get(url)

    # 定位元素
    def locator(self, by, value):
        return self.driver.find_element(by, value)

    # 输入
    def input(self, by, value, txt):
        self.locator(by, value).send_keys(txt)

    # 点击
    def click(self, by, value):
        self.locator(by, value).click()

    # 关闭
    def quit(self):
        self.driver.quit()

    # 强制等待
    def wait(self, time_):
        sleep(int(time_))

    # 断言
    def assert_text(self, expected, by, value):
        reality = self.locator(by, value).text
        assert expected == reality, f'''
    预期结果为{expected}，实际结果为{reality}。
    {expected} != {reality}
'''

    # 提取验证码
    def get_code(self, by, value):
        file = self.locator(by, value).screenshot_as_png
        return ddddocr.DdddOcr(show_ad=False).classification(file)
